<?php
function_exists('UnixPath')  || die('OK');

	$ACT = array('list', 'edit', 'eok', 'del' ,'add', 'aok'); // 
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';
	//$html['tpl_right_act'] = $_GET['act'];	
	
	$_GET['pid']	= $_pid	= (int) $_GET['pid'];
	$_GET['id']		= $_id	= (int) $_GET['id'];

	$T	= new Tab(1);

	//$_PA	= require("./tree/PA.php");
	$_PB	= require(B_DIR . '/_res/tree/PB.php');


	if( !empty($_PB[$_pid]) ){
		$P	= $_PB[$_pid];
	}else{
		$_GET['pid']	= 1;
		$P		= array('栏目设置', 1, &$_PA);
	}

	//E::$DB->_tab = $tab = $T->atrrs['tableName'];

	$tmp	= $_pid == 1  ? '首页' : $P[0];

	$body['navlist'] = array(
		'list' => '子'. $T->attrs['itemName'] .'列表',
		'add'	=>	'添加子'. $T->attrs['itemName']
	);

	$body['navslist'] = array(
		array( '区域', '?mod=' . $_GET['mod'] . '&fun=area&act=list&pid=' . $_GET['pid'], $tmp),
	);

	if(  $_pid > 1 ){
		array_push( $body['navslist'] , 
			array('编辑', '?mod=' . $_GET['mod'] . '&fun=' . $_GET['fun'] . '&act=edit&pid=' . $P[1] . '&id=' . $_GET['pid'], $tmp)
		);
	}


	$body['_url']	= '&pid=' . $_GET['pid'] .'&';
	$body['url']	= $html['url'] . $body['_url'];
	$body['node_tree'] = title_tree($_GET['pid']);


if ( $_GET['act'] ===  'add' ){
	$tabs	= E::$DB->ary('_table_list', '1','*','id');
	$L['tabs_radio']		= tabs_radio($tabs);

	$param_B	= include(B_DIR . '/_res/tree/param_B.php' );
	$L['params_radio']	= param_radio_group($param_B);

	$L['stype'] = $_GET['sid'];
}



if( $_GET['act'] === 'aok' ){
	$x	= E::$DB->one('_super_param', 'pid=' . $_GET['pid'] .' AND paramName=' . code::quote($_POST['paramName']));

	if( $x['id'] ){
		msg('参数 ' . $_POST['paramName'] .' 已经存在!');
	}

	$L		= E::$DB->one('_super_param', 'id=' . $_GET['pid']);
	$_POST['pid']	= $_GET['pid'];
	$_POST['deep']	= $L['deep'] + 1 ;

	$id		=  E::$DB->insert('_super_param', 'pid,isParent,deep,paramName,orderId');

	addColumnToMap($id);

	$T->update('isUseTab,tab,isUseParam,bindId,pid', $id);

	C::param();
	column_cache();
	tpl_cache($id);
	A::msg('添加' . $T->attrs['itemName'] . ' ' .  $_POST['paramName'] . ' 完成!', $_POST['jurl'], 1, 1 );
}



if( $_GET['act'] === 'edit' ){
	$sL	= E::$DB->ary('_super_param', 'pid=' . $_pid, '*', 'id');

	if( empty($sL[$_id]) ) {
		die('Error On File:' . __FILE__ . ' Line:' . __LINE__);	
	}
	$L	= $T->one($_id);

	if( empty($L['id']) ){
		die('Error On File:' . __FILE__ . ' Line:' . __LINE__);
	}

	
	$xL	= E::$DB->one('_super_param', 'id=' .$_GET['id']);

	$tabs	= E::$DB->ary('_table_list', 1, '*', 'id');
	$L['tabs_radio']	= tabs_radio($tabs, $L['tab']);

	
	$param_B	= include(B_DIR . '/_res/tree/param_B.php' );
	$L['params_radio']	= param_radio_group($param_B, $L['bindId']);

	if( $xL['isParent'] ){
		$body['node_tree']	.= '<small>&#187;</small>  <a href="?mod=' . $_GET['mod'] . '&fun=' . $_GET['fun'] . '&act=list&pid=' . $_GET['id'] . '">' . $xL['paramName'] . '</a> ';
	}else{
		$body['node_tree']	.= '<small>&#187;</small> <a>' . $xL['paramName'] . '</a> ';
	}

	//$body['node_tree']	.= '<small>※</small> <a href="?mod=' . $_GET['mod'] . '&fun=area&act=list&pid='  . $_GET['id'] . '"  title="' . $xL['paramName'] .'">区域</a>';
}

if( $_GET['act'] === 'eok' ){
	$L	= $T->one($_GET['id']);
	if( empty($L['id']) ) {
		die('Error On File:' . __FILE__ . ' Line:' . __LINE__);	
	}
	$_POST['pid']	= $_pid;
	$T->update('isUseTab,tab,isUseParam,bindId,pid', $L['id']);

	E::$DB->update('_super_param', 'paramName, orderId', 'id=' .$L['id']);

	C::param();
	column_cache();
	tpl_cache($L['id']);
	A::msg('编辑 ' . $T->attrs['itemName'] . ' ' . $_POST['paramName']  . ' 完成!', $_POST['jurl'], 1, 1 );
}


if( $_GET['act'] === 'list' ){

	$xL	= E::$DB->ary('_super_param', 'pid=' .$_pid . ' ORDER BY orderId DESC, id ASC', '*' , 'id');

	$L	= $T->ary('*', 'pid=' . $_pid );
	//删除不在_super_param里的节点
	foreach($L as $id => &$v){
		if( empty($xL[$id]) ) {
			$T->delete($id);
		}
	}
	unset($v);

	//添加不在Maps里的_super_param节点
	foreach($xL as $id=>&$v){
		if( empty($L[$id]) ){
			addColumnToMap($v);
		}
	}
	unset($v);
		
	$xL	= E::$DB->ary('_super_param', 'pid=' .$_GET['pid'] . ' ORDER BY orderId DESC, id ASC', '*', 'id');

}



if( $_GET['act'] === 'del' ){
	$L	= $T->one($_GET['id']);
	$childs	= E::$DB->all('_super_param', 'pid=' . $L['id'] );
	if( !empty( $childs ) ){
		A::msg('有子栏目的栏目不能被直接删除!', $_POST['jurl'], 1, 1 );
	}
	$T->delete($L['id']);
	E::$DB->query('DELETE FROM _super_param WHERE id='. $L['id']);

	C::param();
	column_cache();
	@unlink(B_DIR . '/_res/_tpl/column/' . $L['id'] . '.txt');
	A::msg('删除栏目完成!', $_POST['jurl'], 1, 1 );
}

	$tpl->assign_by_ref('T', $T->attrs);
	$tpl->assign_by_ref('F', $T->attrs['field']);


	$tpl->assign_by_ref('xL', $xL);

	$tpl->assign_by_ref('P', $P);




/***********************************************************************************************/



function column_cache(){
	global $DB, $T;
	$L1	= E::$DB->ary('_super_param', 
				'pid=1 order BY orderId DESC , id ASC', 
				'id,pid,isParent,paramName');

	$_L	= $T->ary('isUseParam,isUseTab,tab,bindId');

	$L	= array();

	foreach($L1 as $id => $col){
		$ColId	= $col['id'];
		unset($col['id']);
		$col2	= $_L[$id];
		if( $col2['isUseParam'] ){
			$par	= E::$DB->ary('_super_param', 'pid=' . $col2['bindId'] . ' ORDER BY orderId DESC, id ASC', '*', 'id');
			$param	= array();
			foreach($par as $id=>$p){
				$key	= $p['paramValue'] ? $p['paramValue'] :$id;
				$param[$key]	= $p['paramName'];
			}
			$col['param']	= $param;
			$p		= E::$DB->one('_table_field', 'tid=' . $col['tab'] .' AND bindId=' . $col['bindId']);
			$col['key']	= $p['fieldKey'];
		}else{
			$tab['param'] = 0;
		}

		$col['tab']	= $col['isUseTab'] ? $col['tab'] : 0 ;

		unset($col['isUseParam'], $col['isUseTab'], $col['orderId'], $col['namee'], $col['id'] );
		$L[$ColId] = $col;
	}
	//var_dump($L);die;
	hash::save($L, B_DIR . '/_res/const/column.php');
	if( file_exists( B_DIR . '/_column_cache.php' ) ){
		include( B_DIR . '/_column_cache.php' );
	}
}


function addColumnToMap($L){
	if( !is_array($L) ){
		$id	= (int) $L;
		if( !$id ){
			die( 'Error on ' . __FILE__ . ' ' . __LINE__ );	
		}
		$L	= E::$DB->one('_super_param', 'id=' . $id , '*', 'id');
		if( !$L['id'] ){
			die( 'Error on ' . __FILE__ . ' ' . __LINE__ );	
		}
	}
	global $T;
	$x2	= $T->one($L['id']);
	if( empty( $x2['id']) ){
		$sql	= 'INSERT INTO '. $T->attrs['tableName'] . ' (id, pid) VALUE (' . $L['id'] . ', ' . $L['pid'] . ' ) ' ;
		E::$DB->query($sql);
	}
}



function title_tree($id){	
	global $_PB;
	if( $id <= 1 ){
		$str	= '<a href="?mod=' . $_GET['mod'] . '&fun=' . $_GET['fun'] . '&act=list&pid=1">首页</a> ';
	}else{
		$str	= title_tree($_PB[$id][1]).' <small>&#187;</small> <a href="?mod=' . $_GET['mod'] . '&fun=' . $_GET['fun'] . '&act=list&pid=' . $id . '">' . $_PB[$id][0] . '</a> ';
	}
	return $str;
}

function tabs_radio($a, $key=''){
	if( is_array($a) ){
		foreach($a as $k=>$v){
			$isc = ($k == $key) ?' checked="true"' :'';
			$s .= '<label for="tab_' . $k . '"><input type="radio" name="tab" value="' . $k . '" id="tab_' . $k . '" ' . $isc . '/>' . $v['description'] . '</label>';

		}
	}
	return $s;
}

function tpl_cache($id){
	global $T;
	$L	= $T->one($id);
	file_put_contents( B_DIR . '/_res/_tpl/column/' . $L['id'] . '.txt', $L['body']);
}

function param_radio_group($a, $key=''){
	if(is_array($a)){
		foreach($a as $k=>$v){
			if( !$v['isParent'] ){
				continue; 
			}
			$dep = $v['deep'] ;
			$isc = ( $v['id'] == $key ) ?' checked="true"' :'';
			$s .= '<label for="mod_' . $k . '"><input type="radio" name="bindId" value="' . $k . '" id="mod_' . $k . '" ' . $isc . '/>' . $v['paramName'] . '</label>';
			if(is_array($v['child'])){
				$s .= $tab . '<div id="mod_'.$k.'_" class="checkboxdep' . $dep . '">' . param_radio_group($v['child'], $key) . '</div>';
			}
		}
	}
	return $s;
}